package com.medical_houduan.controller;


import com.medical_houduan.commons.Result;
import com.medical_houduan.dto.UserLoginDto;
import com.medical_houduan.pojo.User;
import com.medical_houduan.service.UserService;
import com.medical_houduan.utils.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Tag(name="用户模块",description = "用户模块")
@CrossOrigin//支持跨域，适用前后端分离开发，不然前端vue访问不到

public class UserContioller {


    @Autowired
    private UserService userService;


    //登录逻辑
    @PostMapping("/login")
    @Operation(summary = "登录",description = "前端需要传入的json类型的参数")
    public Result login(@RequestBody UserLoginDto userLoginDto) {
        User user = userService.findByUserName(userLoginDto.getUsername());
        /*System.out.println(userLoginDto.getUsername());
        System.out.println(userLoginDto.getPassword());
        System.out.println(user);*/
        if (user == null) {
            return new Result(401,"用户不存在",null);
        }
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",user.getId());
        claims.put("username",user.getUname());//通过map以键值对形式封装token信息
        String token = JwtUtil.genToken(claims);//生成token，将信息填入
        System.out.println(token);
        return new Result(200,"登录成功",token);//返回JWT token令牌


    }





}
